package com.genesis.yunnanji.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.genesis.yunnanji.bean.MessageEventBean;
import com.youth.xframe.base.XFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;

import butterknife.ButterKnife;

/**
 * 创建日期：2018/9/19 on 17:34
 * 作者:Kite
 * 描述:
 */
public abstract class BaseFragment extends XFragment {

    protected Activity mActivity;

    protected static Bundle getBundle(Object... pramars) {
        Bundle bundle = new Bundle();
        if (pramars != null && pramars.length > 0) {
            for (int i = 0; i < pramars.length; i++) {
                bundle.putSerializable("p" + i, (Serializable) pramars[i]);
            }
        }
        return bundle;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEvent(MessageEventBean messageEvent) {

    }

    public abstract View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = initView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initView() {
        this.mActivity = this.getActivity();
    }

    //region 跳转封装

    public void advance(Class<?> cls, Object... params) {
        this.advance(this.getAdvanceIntent(cls, params));
    }

    public void advance(String clsName, Object... params) {
        this.advance(this.getAdvanceIntent(clsName, params));
    }

    public void advance(Intent intent) {
        this.startActivity(intent);
        this.setStartActivityAnim();
    }

    public void advanceForResult(Class<?> cls, int requestCode, Object... params) {
        this.advanceForResult(this.getAdvanceIntent(cls, params), requestCode);
    }

    public void advanceForResult(String clsName, int requestCode, Object... params) {
        this.advanceForResult(this.getAdvanceIntent(clsName, params), requestCode);
    }

    public void advanceForResult(Intent intent, int requestCode) {
        this.startActivityForResult(intent, requestCode);
        this.setStartActivityAnim();
    }

    private void setStartActivityAnim() {
        if (Build.VERSION.SDK_INT >= 5) {
            this.mActivity.overridePendingTransition(org.bining.footstone.R.anim.view_in_from_right, org.bining.footstone.R.anim.view_out_to_left);
        }

    }

    private Intent getAdvanceIntent(Class<?> cls, Object... params) {
        Intent intent = new Intent();
        intent.setClass(this.mActivity, cls);
        return this.putParams(intent, params);
    }

    private Intent getAdvanceIntent(String clsName, Object... params) {
        Intent intent = new Intent();
        intent.setClassName(this.mActivity, clsName);
        return this.putParams(intent, params);
    }

    private Intent putParams(Intent intent, Object... params) {
        if (intent != null && params != null && params.length > 0) {
            for (int i = 0; i < params.length; ++i) {
                intent.putExtra("p" + i, (Serializable) params[i]);
            }
        }
        return intent;
    }

    //endregion

}
